package com.slmxz.tms.document.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.slmxz.tms.common.base.IBaseEnum;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.core.exception.BizException;
import com.slmxz.tms.common.result.ResultCode;
import com.slmxz.tms.document.enums.LocationTypeEnum;
import com.slmxz.tms.document.mapper.LocationMapper;
import com.slmxz.tms.document.model.entity.Location;
import com.slmxz.tms.document.model.vo.LocationVO;
import com.slmxz.tms.document.service.LocationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import cn.hutool.core.util.StrUtil;

/**
 * 地址档案服务实现类
 * @author sunyao
 * @Date 2024-11-08
 */
@Service
@RequiredArgsConstructor
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements LocationService {

    /**
    * 获取地址档案分页列表
    * @return {@link IPage< LocationVO >} 地址档案分页列表
    */
    @Override
    public IPage<LocationVO> selectLocationPage(IPage<LocationVO> page,LocationVO location) {
        List<LocationVO> locations = this.baseMapper.selectLocationPage(page,location);
        locations.forEach(item -> {
            item.setLocationTypeLabel(IBaseEnum.getLabelByValue(item.getLocationType(), LocationTypeEnum.class));
        });
        return page.setRecords(locations);
    }

    @Override
    public List<Location> selectOption(LocationVO location) {
        return this.baseMapper.selectOption(location);
    }

    /**
     * @param id 地址档案ID
     * @return
     */
    @Override
    public LocationVO selectLocationDetail(Long id) {
        Location entity = this.getById(id);
        LocationVO location = BeanUtil.copyProperties(entity,LocationVO.class);
        return location;
    }
    
    /**
     * 新增地址档案
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveLocation(Location location) {
        return this.save(location);
    }
    
    /**
     * 更新地址档案
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateLocation(Location location) {
        return this.updateById(location);
    }
    
    /**
     * 删除地址档案
     *
     * @param ids 地址档案ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteLocations(String ids) {
        Asserts.fail(StrUtil.isBlank(ids), BizMessageEnum.PARAM_IS_NULL);
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeOrLogout(LocationVO location) {
        //激活或者注销业务数据
        Asserts.fail((CollectionUtil.isEmpty(location.getIds()) || location.getIsDeleted() == null), BizMessageEnum.PARAM_IS_NULL);

        List<Location>  locationList = new ArrayList<>(location.getIds().size());
        location.getIds().forEach(id -> {
            Location locationParam = new Location();
            locationParam.setId(id);
            locationParam.setIsDeleted(location.getIsDeleted());
            locationList.add(locationParam);
        });
        this.updateBatchById(locationList,10);
        return true;
    }

}
